package com.googlecode.perfectable.eventable.routing;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.LinkedList;
import java.util.List;

public final class Buffer<E> implements Sink<E> {
	private final List<E> elements = new LinkedList<>();

	Buffer() {
	}

	public static <X> Buffer<X> create() {
		return new Buffer<>();
	}

	@Override
	public void receive(E element) {
		this.elements.add(element);
	}

	public void flush(Sink<E> other) {
		checkNotNull(other);
		for(E element : this.elements) {
			other.receive(checkNotNull(element));
		}
		this.elements.clear();
	}
}
